package security.diffie.hellmann;

import java.math.BigInteger;
import java.util.Random;

/**
 * Créer un thread pour chercher un nombre premier.
 * @author Jean-François Augé
 */
public class PrimeThread extends Thread {

	private static int SIZE;
	private static int winner = 0;
	private static Random rng = BigInteger_.initRNG();
	
	private int name;
	private BigInteger b;
	
	/**
	 * Constructeur.
	 * @param name
	 * @param size
	 */
	@SuppressWarnings("static-access")
	public PrimeThread(int name, int size) {
		this.SIZE = size;
		this.name = name;
		b = BigInteger_.generate_odd(rng, SIZE);
	}

	@Override
	public void run() {
		while(winner == 0) {
			if(BigInteger_.fermat(b) == true) {
				winner = name;
				break;
			}
			b = BigInteger_.generate_odd(rng, SIZE);
		}
	}
	
	public static int getWinner() { return winner; }
	public static void setWinner(int winner) { PrimeThread.winner = winner; }

	public BigInteger getB() { return b; }
	public void setB(BigInteger b) { this.b = b; }

}
